package com.dream.dynamic_datasource.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: Luo
 * @create: 2020-06-22 15:17
 */
@Component
public class RedisUtil {
    @Autowired
    private ValueOperations<String,String> valueOperations;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * String类型无过期时间
     * @param key
     * @param value
     * @date 2020/7/16 14:00
     */
    public void setString(String key,String value){
        valueOperations.set(key,value);
    }
    /**
     * String有过期时间
     * @param key
     * @param value
     * @param expTime
     * @date 2020/7/16 14:00
     */
    public void setStringExpTime(String key,String value,Long expTime){
        valueOperations.set(key, value, expTime, TimeUnit.MILLISECONDS);
    }
    /**
     * String获取值
     * @param key
     * @date 2020/7/16 14:00
     */
    public  String getString(String key){
        return   valueOperations.get(key);
    }
    /**
     * 删除key
     * @param key
     * @date 2020/7/16 14:05
     */
    public boolean deleteKey(String key){
        return redisTemplate.delete(key);
    }
}
